<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            display: flex;
            justify-content: space-around;
            align-items: center;
            width: 400px;
            height: 150px;
            margin: 100px auto;
            background-color: #ccc;
            border-radius: 40px;
        }

        .box div {
            width: 80px;
            height: 80px;
            background-color: #eee;
            /* background-color: green; */
            border-radius: 50%;
        }



        .box .num {
            background-color: #ccc;
            border-radius: 0;
            font-size: 60px;
            line-height: 80px;
            text-align: center;
            color: #fff;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="red"></div>
        <div class="yellow"></div>
        <div class="green"></div>
        <div class="num">30</div>
    </div>

    <script>

        function green(time) {
            var timer = setInterval(lamp, 1000);
            function lamp() {
                if (time > 0) {
                    time--;
                    time = time < 10 ? '0' + time : time;
                    greenLamp.style.backgroundColor = 'green';
                    return num.innerHTML = time;
                } else {
                    greenLamp.style.backgroundColor = '#eee';
                    return clearInterval(timer);
                }
            }
        }
        function yellow(time) {
            var timer = setInterval(lamp, 1000);
            function lamp() {
                if (time > 0) {
                    time--;
                    time = time < 10 ? '0' + time : time;
                    yellowLamp.style.backgroundColor = 'yellow';
                    return num.innerHTML = time;
                } else {
                    yellowLamp.style.backgroundColor = '#eee';
                    return clearInterval(timer);
                }
            }
        }
        function red(time) {
            var timer = setInterval(lamp, 1000);
            function lamp() {
                if (time > 0) {
                    time--;
                    time = time < 10 ? '0' + time : time;
                    redLamp.style.backgroundColor = 'red';
                    return num.innerHTML = time;
                } else {
                    redLamp.style.backgroundColor = '#eee';
                    return clearInterval(timer);
                }
            }
        }

        setTimeout(function () {
            green(5);
        }, 0);
        setTimeout(function () {
            yellow(2)
        }, 5000);
        setTimeout(function () {
            red(5)
        }, 7000);
        setInterval(function () {
            setTimeout(function () {
                green(5);
            }, 0);
            setTimeout(function () {
                yellow(2)
            }, 5000);
            setTimeout(function () {
                red(5)
            }, 7000);
        }, 12000);











        var greenLamp = document.querySelector('.green');
        var yellowLamp = document.querySelector('.yellow');
        var redLamp = document.querySelector('.red');

        var num = document.querySelector('.num');

        greenLamp.onclick = function () {
            alert(111);
        }
    </script>


</body>

</html>